package com.gaojiawei.multiThread05_ThreeSpecialAtomic;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 使用CAS保证原子性
 */
public class l03_AtomicWithCAS {

    private AtomicInteger count = new AtomicInteger(0);

    @Test
    public void testAtomicWithCAS() throws InterruptedException {
        Thread thread = new Thread(() -> {
            sync();
        });

        Thread thread1 = new Thread(() -> {
            sync();
        });

        thread.start();
        thread1.start();
        Thread.sleep(3000);
        System.out.println(count);
    }

    public void sync(){
        for(int i = 0;i<100;i++){
            count.incrementAndGet();
        }
    }
}
